package com.lz.library.extension

import android.graphics.drawable.Drawable
import android.widget.Toast
import androidx.core.content.ContextCompat

/**
 *  author: you : 2020/05/4
 */
/**
 * dp 转 px
 * @return
 */
fun Float.dp2px(): Int {
    val scale = APP.resources.displayMetrics.density
    return (this * scale + 0.5f).toInt()
}

fun Int.dp2px(): Int {
    val scale = APP.resources.displayMetrics.density
    return (this * scale + 0.5f).toInt()
}

/**
 * px 转 dp
 * @return
 */
fun Float.px2dp(): Int {
    val scale = APP.resources.displayMetrics.density
    return (this / scale + 0.5f).toInt()
}

fun Int.px2dp(): Int {
    val scale = APP.resources.displayMetrics.density
    return (this / scale + 0.5f).toInt()
}

/**
 * sp 转 px
 * @return
 */
fun Float.sp2px(): Int {
    val fontScale = APP.resources.displayMetrics.scaledDensity
    return (this * fontScale + 0.5f).toInt()
}

fun Int.sp2px(): Int {
    val fontScale = APP.resources.displayMetrics.scaledDensity
    return (this * fontScale + 0.5f).toInt()
}

/**
 * px 转 sp
 * @return
 */
fun Float.px2sp(): Int {
    val fontScale = APP.resources.displayMetrics.scaledDensity
    return (this / fontScale + 0.5f).toInt()
}

fun Int.px2sp(): Int {
    val fontScale = APP.resources.displayMetrics.scaledDensity
    return (this / fontScale + 0.5f).toInt()
}

/**
 * 二进制标记 0 1
 */
fun Int.binaryIndex(): Int = 0x1 shl this
fun Int.binaryIndex(index: Int) = this shr index and 0x1

/**
 * Toast 显示string
 */
fun String.showToast(): Unit = Toast.makeText(APP, this, Toast.LENGTH_SHORT).show()
fun String.showLongToast(): Unit = Toast.makeText(APP, this, Toast.LENGTH_LONG).show()

/**
 * Toast 显示StringRes
 */
fun Int.showToast(): Unit = Toast.makeText(APP, this, Toast.LENGTH_SHORT).show()
fun Int.showLongToast(): Unit = Toast.makeText(APP, this, Toast.LENGTH_LONG).show()

//res
fun Int.resString(): String = APP.getString(this)
fun Int.drawable(): Drawable? = ContextCompat.getDrawable(APP, this)
fun Int.color(): Int = ContextCompat.getColor(APP, this)